package com.hl.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hl.domain.User;
import com.hl.service.UserService;
import com.hl.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.apache.ibatis.ognl.DynamicSubscript.all;


@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

//    查询全部信息
    @GetMapping
    public R UserController(int page, int size) {
        return  R.success(userService.findAll(page, size));
    }
//    条件模糊搜索
    @GetMapping("/condition")
    public R findUser(Integer page, Integer size , String username, String nickname) {
        if (page == null) page = 1;
        if (size == null) size = 10;
        return  R.success(userService.findByCondition(page, size,username,nickname));
    }

//    添加数据
    @PostMapping("/update")
    public R updateUser(String username, String password ,String nickname,String email,String phone,int role) {
        User user =new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setNickname(nickname);
        user.setEmail(email);
        user.setPhone(phone);
        user.setRole(role);
        return R.success(userService.addUser(user));
    }
//    修改
    @PutMapping
    public R updateUser1(@RequestBody User user) {
        return R.success(userService.updateUser(user));
    }

//    删除
    @DeleteMapping
    public R deleteUser(Long id) {
        return R.success(userService.deleteUser(id));
    }

  //根据用户名查询用户
    @GetMapping("/login")
    public R selectUserLogin(String username, String password){
        User user = userService.selectUserLogin(username, password);
        return user != null?R.success(user) : R.fail(null);
    }

//    查询用户和咨询回复列表
    @GetMapping("/selectUserAndConsultReply")
    public R selectUserAndConsultReply(int pageNum, int pageSize){
        return R.success(userService.selectUserAndConsultReply(pageNum,pageSize));
    }

 @GetMapping("/loginzjy")
    public R login(String username, String password){
        List<User> users = userService.login(username,password);
        System.out.println(users);
        return users.isEmpty() ?R.fail("登录失败"):R.success("登录成功");
    }

    //注册
    @PostMapping("register")
    public R register(User user){
        int i = userService.register(user);
        return i==0?R.fail("注册失败"):R.success("注册成功");
    }

    //根据电话号查询
    @GetMapping("selectByNumber")
    public int selectByNumber(String phone){
        List<User> users = userService.selectByNumber(phone);
        System.out.println(users);
        return users.isEmpty()?0 :1;
    }


    //根据用户名或者手机查询
    @GetMapping("selectNameOrNumber")
    public Map<String, String> selectNameOrNumber(String username){
        User user = userService.selectNameOrNumber(username);
        Map<String, String> result = new HashMap<>();
        String phone = user.getPhone();
        String username1 = user.getUsername();
        result.put("phone",phone);
        result.put("username",username1);

        return result;
    }

    //根据用户名更改手机号
    @PostMapping ("updatePhoneByName")
    public int updatePhoneByName(String username, String phone){
        int i = userService.updatePhoneByName(username,phone);
        return i==0?0:1;
    }


    //根据用户名查找密码
    @GetMapping("selectPwdByUsername")
    public int selectPwdByUsername(String username,String password){

        int i = userService.selectPwdByUsername(username,password);

        return i==0?0:1;
    }

    //根据用户名修改密码
    @PostMapping("updatePwdByUserNumber")
    public int updatePwdByUserNumber(String username, String password){
        System.out.println("888888" + username);
        int i = userService.updatePwdByUserNumber(username,password);
        return i==0?0:1;
    }

    //根据用户名获取id
    @GetMapping("selectIdByUsername")
    public List<User> selectIdByUsername(String username){
        return userService.selectIdByUsername(username);
    }


}
